﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Threading.Tasks;
using Castle.Core.Internal;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Services;
using Essensoft.Paylink.Alipay.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Nito.AsyncEx;
using SqlSugar;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 产品订单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private IHttpContextUser _user;
        private readonly AsyncLock _mutex = new AsyncLock();

        private IXcrProductsServices _xcrProductsServices;
        private IXcrProductsTypeServices _xcrProductsTypeServices;
        private ICoreCmsAreaServices _coreCmsAreaServices;
        private IXrcOrderServices _xrcOrderServices;
        private ICoreCmsUserServices _coreCmsUserServices;
        private IXrcApplyBrokerServices _xrcApplyBrokerServices;
        private ICoreCmsUserWeChatInfoServices _coreCmsUserWeChatInfoServices;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="user"></param>
        /// <param name="xcrProductsServices"></param>
        /// <param name="xcrProductsTypeServices"></param>
        public OrderController(IHttpContextUser user, IXcrProductsServices xcrProductsServices, IXcrProductsTypeServices xcrProductsTypeServices, ICoreCmsAreaServices coreCmsAreaServices, IXrcOrderServices xrcOrderServices, ICoreCmsUserServices coreCmsUserServices, IXrcApplyBrokerServices xrcApplyBrokerServices, ICoreCmsUserWeChatInfoServices coreCmsUserWeChatInfoServices)
        {
            _user = user;
            _xcrProductsServices = xcrProductsServices;
            _xcrProductsTypeServices = xcrProductsTypeServices;
            _coreCmsAreaServices = coreCmsAreaServices;
            _xrcOrderServices = xrcOrderServices;
            _coreCmsUserServices = coreCmsUserServices;
            _xrcApplyBrokerServices = xrcApplyBrokerServices;
            _coreCmsUserWeChatInfoServices = coreCmsUserWeChatInfoServices;

        }
        #region 订单列表
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<WebApiCallBack> OrderList([FromBody] FMOrderList entity)
        {
            var jm = new WebApiCallBack();

            Expression<Func<XrcOrder, bool>> where = m => m.Delete == false && m.UserId == _user.ID;
            if (entity.state > 0)
            {
                where = where.And(p => p.State == entity.state);
            }
            if (entity.isloin > 0)
            {
                where = where.And(p => p.IsLoin == entity.isloin);
            }
            var list = await _xrcOrderServices.QueryPageAsync(where, p => p.AddTime, OrderByType.Desc,
                entity.page, entity.limit);
            jm.status = true;
            jm.data = list;
            return jm;
        }

        #endregion

        /// <summary>
        /// 获取单个订单详情
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        // [Authorize]
        public async Task<WebApiCallBack> OrderInfo([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _xrcOrderServices.QueryByIdAsync(entity.id);
            if (model == null)
            {
                jm.msg = "数据获取失败";
                return jm;
            }
            else
            {
                var type = new XcrProductsType();
                if (model.ProType > 0)
                    type = _xcrProductsTypeServices.QueryById(model.ProType);
                else
                    type.type_name = "测额度";

                var state = model.State == 0 ? "待审核" : model.State == 1 ? "申请成功" : "申请失败";
                var isloin = model.IsLoin == 0 ? "未放款" : "已放款";
                string brokermobile = "";//推荐人手机号
                string brokername = "";//推荐人姓名
                if (model.BrokerId > 0)
                {
                    var tuijianren = _xrcApplyBrokerServices.QueryByClause(p => p.Userid == model.BrokerId);
                    if (tuijianren != null)
                    {
                        brokermobile = tuijianren.Mobile;
                        brokername = tuijianren.Name;
                    }
                    else
                    {
                        var user = _coreCmsUserWeChatInfoServices.QueryById(model.BrokerId);
                        if (user != null)
                        {
                            brokermobile = user.mobile;
                            brokername = user.nickName;
                        }
                    }
                }
                jm.data = new
                {
                    model.Id,
                    model.AddTime,
                    model.ProName,
                    state,
                    type.type_name,
                    isloin,
                    brokername,
                    brokermobile,
                    model.Name,
                    model.Mobile,
                    model.ExpQuota,
                    model.RealityQuota,
                    model.Remark
                };
                jm.status = true;
                return jm;

            }

        }



    }
}
